package com.git.androidwy.UI.splitView;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;

import com.git.androidwy.R;

import java.util.ArrayList;
import java.util.List;

public class SplitView extends View {
    private Bitmap mBitmap;
    private List<Ball> mBalls = new ArrayList<>();
    private float d = 3.0f;
    private ValueAnimator valueAnimator;
    private Paint mPaint = new Paint();

    public SplitView(Context context) {
        this(context, null);
    }

    public SplitView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SplitView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    private void init(){
        mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.tx1);
        for (int i = 0 ; i < 100 ; i++){
            for (int j = 0 ; j < 100 ; j++){
                Ball tempBall = new Ball();
                tempBall.x = i * d + d / 2;
                tempBall.y = j * d + d / 2;
                tempBall.r = d / 2;
                tempBall.color = mBitmap.getPixel(i, j);
                //速度(-20,20)
                tempBall.vx = (float) (Math.pow(-1, Math.ceil(Math.random() * 1000)) * 20 * Math.random());
                tempBall.vy = rangInt(-15, 35);
                tempBall.ax = 0;
                tempBall.ay = -0.98f;
                mBalls.add(tempBall);
            }
        }
        valueAnimator = ValueAnimator.ofFloat(0, 1);
        valueAnimator.setRepeatCount(-1);
        valueAnimator.setDuration(2000);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                updateBalls();
                invalidate();
            }
        });
    }
    private void updateBalls(){
        for (int i = 0 ; i < mBalls.size() ; i++){
            Ball ball = mBalls.get(i);
            ball.x += ball.vx;
            ball.y += ball.vy;
            ball.vx += ball.ax;
            ball.vy += ball.ay;
        }
    }
    private float rangInt(int i, int j) {
        int min = Math.min(i, j);
        int max = Math.max(i, j);
        return (float) (min + Math.random() * (max - min));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0 ; i < mBalls.size() ; i++){
            mPaint.setColor(mBalls.get(i).color);
            canvas.drawCircle(mBalls.get(i).x, mBalls.get(i).y, mBalls.get(i).r, mPaint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN){
            valueAnimator.start();
        }
        return super.onTouchEvent(event);
    }
}
